<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Stock {

    private $stockStr;
    private $stockArray;
    private $stockArrayTurn;

    public function __construct() {
        $this->stockStr = file_get_contents(dirname(__DIR__) . '/cache/utf8Stock.txt');
        $this->stockArray = unserialize($this->stockStr);
        $this->stockArrayTurn = array_flip($this->stockArray);
    }

    public function checkName($stockName) {
        $stockName = trim($stockName);
        if (in_array($stockName, $this->stockArray)) {
            $data['stockName'] = $stockName;
            $data['stockCode'] = $this->stockArrayTurn[$stockName];
            return $data;
        } else {
            return false;
        }
    }

    public function checkCode($stockCode) {
        $stockCode = trim($stockCode);
        if (key_exists($stockCode, $this->stockArray)) {
            $data['stockName'] = $this->stockArray[$stockCode];
            $data['stockCode'] = $stockCode;
            return $data;
        } else {
            return false;
        }
        
    }

}

